{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 271,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#Apply进行数据预处理\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "df1 = pd.read_csv('年度数据.csv',encoding='gb2312')\n",
    "# df1['2021年'].apply(lambda x:x*2 ) #对单个元素进行处理的元素\n",
    "        # Examples\n",
    "        # --------\n",
    "        # Create a series with typical summer temperatures for each city.\n",
    "        #\n",
    "        # >>> s = pd.Series([20, 21, 12],\n",
    "        # ...               index=['London', 'New York', 'Helsinki'])\n",
    "        # >>> s\n",
    "        # London      20\n",
    "        # New York    21\n",
    "        # Helsinki    12\n",
    "        # dtype: int64\n",
    "        #\n",
    "        # Square the values by defining a function and passing it as an\n",
    "        # argument to ``apply()``.\n",
    "        #\n",
    "        # >>> def square(x):\n",
    "        # ...     return x ** 2\n",
    "        # >>> s.apply(square)\n",
    "        # London      400\n",
    "        # New York    441\n",
    "        # Helsinki    144\n",
    "        # dtype: int64\n",
    "        #\n",
    "        # Square the values by passing an anonymous function as an\n",
    "        # argument to ``apply()``.\n",
    "        #\n",
    "        # >>> s.apply(lambda x: x ** 2)\n",
    "        # London      400\n",
    "        # New York    441\n",
    "        # Helsinki    144\n",
    "        # dtype: int64\n",
    "        #\n",
    "        # Define a custom function that needs additional positional\n",
    "        # arguments and pass these additional arguments using the\n",
    "        # ``args`` keyword.\n",
    "        #\n",
    "        # >>> def subtract_custom_value(x, custom_value):\n",
    "        # ...     return x - custom_value\n",
    "        #\n",
    "        # >>> s.apply(subtract_custom_value, args=(5,))\n",
    "        # London      15\n",
    "        # New York    16\n",
    "        # Helsinki     7\n",
    "        # dtype: int64\n",
    "        #\n",
    "        # Define a custom function that takes keyword arguments\n",
    "        # and pass these arguments to ``apply``.\n",
    "        #\n",
    "        # >>> def add_custom_values(x, **kwargs):\n",
    "        # ...     for month in kwargs:\n",
    "        # ...         x += kwargs[month]\n",
    "        # ...     return x\n",
    "        #\n",
    "        # >>> s.apply(add_custom_values, june=30, july=20, august=25)\n",
    "        # London      95\n",
    "        # New York    96\n",
    "        # Helsinki    87\n",
    "        # dtype: int64\n",
    "        #\n",
    "        # Use a function from the Numpy library.\n",
    "        #\n",
    "        # >>> s.apply(np.log)\n",
    "        # London      2.995732\n",
    "        # New York    3.044522\n",
    "        # Helsinki    2.484907\n",
    "        # dtype: float64\n",
    "        # \"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 272,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>指标</th>\n",
       "      <th>2021年</th>\n",
       "      <th>2020年</th>\n",
       "      <th>2019年</th>\n",
       "      <th>2018年</th>\n",
       "      <th>2017年</th>\n",
       "      <th>2016年</th>\n",
       "      <th>2015年</th>\n",
       "      <th>2014年</th>\n",
       "      <th>2013年</th>\n",
       "      <th>2012年</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>居民人均消费支出(元)</td>\n",
       "      <td>24100.0</td>\n",
       "      <td>21210.0</td>\n",
       "      <td>21559.0</td>\n",
       "      <td>19853.0</td>\n",
       "      <td>18322.0</td>\n",
       "      <td>17111.0</td>\n",
       "      <td>15712.0</td>\n",
       "      <td>14491.0</td>\n",
       "      <td>13220.0</td>\n",
       "      <td>12054.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>居民人均消费支出比上年增长(%)</td>\n",
       "      <td>12.6</td>\n",
       "      <td>-4.0</td>\n",
       "      <td>5.5</td>\n",
       "      <td>6.2</td>\n",
       "      <td>5.4</td>\n",
       "      <td>6.8</td>\n",
       "      <td>6.9</td>\n",
       "      <td>7.5</td>\n",
       "      <td>6.9</td>\n",
       "      <td>8.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>居民人均服务性消费支出(元)</td>\n",
       "      <td>10645.0</td>\n",
       "      <td>9037.0</td>\n",
       "      <td>9886.0</td>\n",
       "      <td>8781.0</td>\n",
       "      <td>7803.0</td>\n",
       "      <td>7157.0</td>\n",
       "      <td>6460.0</td>\n",
       "      <td>5842.0</td>\n",
       "      <td>5246.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>居民人均服务性消费支出比上年增长(%)</td>\n",
       "      <td>17.8</td>\n",
       "      <td>-8.6</td>\n",
       "      <td>12.6</td>\n",
       "      <td>12.5</td>\n",
       "      <td>9.0</td>\n",
       "      <td>10.8</td>\n",
       "      <td>10.6</td>\n",
       "      <td>11.4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>居民人均食品烟酒支出(元)</td>\n",
       "      <td>7178.0</td>\n",
       "      <td>6397.0</td>\n",
       "      <td>6084.0</td>\n",
       "      <td>5631.0</td>\n",
       "      <td>5374.0</td>\n",
       "      <td>5151.0</td>\n",
       "      <td>4814.0</td>\n",
       "      <td>4494.0</td>\n",
       "      <td>4127.0</td>\n",
       "      <td>3983.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>居民人均食品烟酒支出比上年增长(%)</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.1</td>\n",
       "      <td>8.0</td>\n",
       "      <td>4.8</td>\n",
       "      <td>4.3</td>\n",
       "      <td>7.0</td>\n",
       "      <td>7.1</td>\n",
       "      <td>8.9</td>\n",
       "      <td>3.6</td>\n",
       "      <td>9.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>居民人均衣着支出(元)</td>\n",
       "      <td>1419.0</td>\n",
       "      <td>1238.0</td>\n",
       "      <td>1338.0</td>\n",
       "      <td>1289.0</td>\n",
       "      <td>1238.0</td>\n",
       "      <td>1203.0</td>\n",
       "      <td>1164.0</td>\n",
       "      <td>1099.0</td>\n",
       "      <td>1027.0</td>\n",
       "      <td>992.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>居民人均居住支出(元)</td>\n",
       "      <td>5641.0</td>\n",
       "      <td>5215.0</td>\n",
       "      <td>5055.0</td>\n",
       "      <td>4647.0</td>\n",
       "      <td>4107.0</td>\n",
       "      <td>3746.0</td>\n",
       "      <td>3419.0</td>\n",
       "      <td>3201.0</td>\n",
       "      <td>2999.0</td>\n",
       "      <td>2480.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>居民人均生活用品及服务支出(元)</td>\n",
       "      <td>1423.0</td>\n",
       "      <td>1260.0</td>\n",
       "      <td>1281.0</td>\n",
       "      <td>1223.0</td>\n",
       "      <td>1121.0</td>\n",
       "      <td>1044.0</td>\n",
       "      <td>951.0</td>\n",
       "      <td>890.0</td>\n",
       "      <td>806.0</td>\n",
       "      <td>741.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>居民人均交通通信支出(元)</td>\n",
       "      <td>3156.0</td>\n",
       "      <td>2762.0</td>\n",
       "      <td>2862.0</td>\n",
       "      <td>2675.0</td>\n",
       "      <td>2499.0</td>\n",
       "      <td>2338.0</td>\n",
       "      <td>2087.0</td>\n",
       "      <td>1869.0</td>\n",
       "      <td>1627.0</td>\n",
       "      <td>1451.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>居民人均教育文化娱乐支出(元)</td>\n",
       "      <td>2599.0</td>\n",
       "      <td>2032.0</td>\n",
       "      <td>2513.0</td>\n",
       "      <td>2226.0</td>\n",
       "      <td>2086.0</td>\n",
       "      <td>1915.0</td>\n",
       "      <td>1723.0</td>\n",
       "      <td>1536.0</td>\n",
       "      <td>1398.0</td>\n",
       "      <td>1262.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>居民人均医疗保健支出(元)</td>\n",
       "      <td>2115.0</td>\n",
       "      <td>1843.0</td>\n",
       "      <td>1902.0</td>\n",
       "      <td>1685.0</td>\n",
       "      <td>1451.0</td>\n",
       "      <td>1307.0</td>\n",
       "      <td>1165.0</td>\n",
       "      <td>1045.0</td>\n",
       "      <td>912.0</td>\n",
       "      <td>838.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>居民人均其他用品及服务支出(元)</td>\n",
       "      <td>569.0</td>\n",
       "      <td>462.0</td>\n",
       "      <td>524.0</td>\n",
       "      <td>477.0</td>\n",
       "      <td>447.0</td>\n",
       "      <td>406.0</td>\n",
       "      <td>389.0</td>\n",
       "      <td>358.0</td>\n",
       "      <td>325.0</td>\n",
       "      <td>307.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     指标    2021年    2020年    2019年    2018年    2017年    2016年  \\\n",
       "0           居民人均消费支出(元)  24100.0  21210.0  21559.0  19853.0  18322.0  17111.0   \n",
       "1      居民人均消费支出比上年增长(%)     12.6     -4.0      5.5      6.2      5.4      6.8   \n",
       "2        居民人均服务性消费支出(元)  10645.0   9037.0   9886.0   8781.0   7803.0   7157.0   \n",
       "3   居民人均服务性消费支出比上年增长(%)     17.8     -8.6     12.6     12.5      9.0     10.8   \n",
       "4         居民人均食品烟酒支出(元)   7178.0   6397.0   6084.0   5631.0   5374.0   5151.0   \n",
       "5    居民人均食品烟酒支出比上年增长(%)      NaN      5.1      8.0      4.8      4.3      7.0   \n",
       "6           居民人均衣着支出(元)   1419.0   1238.0   1338.0   1289.0   1238.0   1203.0   \n",
       "8           居民人均居住支出(元)   5641.0   5215.0   5055.0   4647.0   4107.0   3746.0   \n",
       "10     居民人均生活用品及服务支出(元)   1423.0   1260.0   1281.0   1223.0   1121.0   1044.0   \n",
       "12        居民人均交通通信支出(元)   3156.0   2762.0   2862.0   2675.0   2499.0   2338.0   \n",
       "14      居民人均教育文化娱乐支出(元)   2599.0   2032.0   2513.0   2226.0   2086.0   1915.0   \n",
       "16        居民人均医疗保健支出(元)   2115.0   1843.0   1902.0   1685.0   1451.0   1307.0   \n",
       "18     居民人均其他用品及服务支出(元)    569.0    462.0    524.0    477.0    447.0    406.0   \n",
       "\n",
       "      2015年    2014年    2013年    2012年  \n",
       "0   15712.0  14491.0  13220.0  12054.0  \n",
       "1       6.9      7.5      6.9      8.6  \n",
       "2    6460.0   5842.0   5246.0      NaN  \n",
       "3      10.6     11.4      NaN      NaN  \n",
       "4    4814.0   4494.0   4127.0   3983.0  \n",
       "5       7.1      8.9      3.6      9.6  \n",
       "6    1164.0   1099.0   1027.0    992.0  \n",
       "8    3419.0   3201.0   2999.0   2480.0  \n",
       "10    951.0    890.0    806.0    741.0  \n",
       "12   2087.0   1869.0   1627.0   1451.0  \n",
       "14   1723.0   1536.0   1398.0   1262.0  \n",
       "16   1165.0   1045.0    912.0    838.0  \n",
       "18    389.0    358.0    325.0    307.0  "
      ]
     },
     "execution_count": 272,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#数据的去重 Series的unique方法\n",
    "df1['2021年'].unique() #查看独特的\n",
    "df1['2021年'].duplicated() #查看是否为重复，显示True或False\n",
    "df1.drop_duplicates(['2021年']) #扔掉重复项"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 273,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2022-10-02    1.655690\n",
       "2012-10-02   -0.242178\n",
       "2042-10-02    0.150500\n",
       "2012-10-22   -0.219783\n",
       "dtype: float64"
      ]
     },
     "execution_count": 273,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#处理时间序列\n",
    "from datetime import datetime\n",
    "t1 = datetime(2022,10,2)\n",
    "datelist = [datetime(2022,10,2)\n",
    "            ,datetime(2012,10,2)\n",
    "            ,datetime(2042,10,2)\n",
    "            ,datetime(2012,10,22)]\n",
    "s1 = pd.Series(np.random.randn(4),index=datelist)\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 274,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.6556900535760428"
      ]
     },
     "execution_count": 274,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1[datetime(2022,10,2)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 275,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.6556900535760428"
      ]
     },
     "execution_count": 275,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1['2022-10-2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 276,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.6556900535760428"
      ]
     },
     "execution_count": 276,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1['20221002']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 277,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.23098071908484796"
      ]
     },
     "execution_count": 277,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1['2012-10'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 278,
   "metadata": {},
   "outputs": [],
   "source": [
    "#数据分箱技术Binning\n",
    "bins = [0,59,70,80,10000,20000]\n",
    "category = pd.cut(df1['2021年'],bins)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 279,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(80, 10000]       8\n",
       "(0, 59]           2\n",
       "(10000, 20000]    1\n",
       "(59, 70]          0\n",
       "(70, 80]          0\n",
       "Name: 2021年, dtype: int64"
      ]
     },
     "execution_count": 279,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.value_counts(category)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 280,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>city</th>\n",
       "      <th>temperature</th>\n",
       "      <th>wind</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>03/01/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>8</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>17/01/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>12</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>31/01/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>19</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>14/02/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>-3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>28/02/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>19</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>13/03/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>27/03/2016</td>\n",
       "      <td>SH</td>\n",
       "      <td>-4</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>10/04/2016</td>\n",
       "      <td>SH</td>\n",
       "      <td>19</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>24/04/2016</td>\n",
       "      <td>SH</td>\n",
       "      <td>20</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>08/05/2016</td>\n",
       "      <td>SH</td>\n",
       "      <td>17</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>22/05/2016</td>\n",
       "      <td>SH</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>05/06/2016</td>\n",
       "      <td>SH</td>\n",
       "      <td>-10</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>19/06/2016</td>\n",
       "      <td>SH</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>03/07/2016</td>\n",
       "      <td>SH</td>\n",
       "      <td>-9</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>17/07/2016</td>\n",
       "      <td>GZ</td>\n",
       "      <td>10</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>31/07/2016</td>\n",
       "      <td>GZ</td>\n",
       "      <td>-1</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>14/08/2016</td>\n",
       "      <td>GZ</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>28/08/2016</td>\n",
       "      <td>GZ</td>\n",
       "      <td>25</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>11/09/2016</td>\n",
       "      <td>SZ</td>\n",
       "      <td>20</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>25/09/2016</td>\n",
       "      <td>SZ</td>\n",
       "      <td>-10</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          date city  temperature  wind\n",
       "0   03/01/2016   BJ            8     5\n",
       "1   17/01/2016   BJ           12     2\n",
       "2   31/01/2016   BJ           19     2\n",
       "3   14/02/2016   BJ           -3     3\n",
       "4   28/02/2016   BJ           19     2\n",
       "5   13/03/2016   BJ            5     3\n",
       "6   27/03/2016   SH           -4     4\n",
       "7   10/04/2016   SH           19     3\n",
       "8   24/04/2016   SH           20     3\n",
       "9   08/05/2016   SH           17     3\n",
       "10  22/05/2016   SH            4     2\n",
       "11  05/06/2016   SH          -10     4\n",
       "12  19/06/2016   SH            0     5\n",
       "13  03/07/2016   SH           -9     5\n",
       "14  17/07/2016   GZ           10     2\n",
       "15  31/07/2016   GZ           -1     5\n",
       "16  14/08/2016   GZ            1     5\n",
       "17  28/08/2016   GZ           25     4\n",
       "18  11/09/2016   SZ           20     1\n",
       "19  25/09/2016   SZ          -10     4"
      ]
     },
     "execution_count": 280,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#数据分组技术groupby\n",
    "df1 = pd.read_csv('city_weather.csv')\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 281,
   "metadata": {},
   "outputs": [],
   "source": [
    "g = df1.groupby(df1['city'])#返回对象拿一个新的做类比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 282,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'BJ': [0, 1, 2, 3, 4, 5], 'GZ': [14, 15, 16, 17], 'SH': [6, 7, 8, 9, 10, 11, 12, 13], 'SZ': [18, 19]}"
      ]
     },
     "execution_count": 282,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g.groups"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 283,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>city</th>\n",
       "      <th>temperature</th>\n",
       "      <th>wind</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>03/01/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>8</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>17/01/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>12</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>31/01/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>19</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>14/02/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>-3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>28/02/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>19</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>13/03/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         date city  temperature  wind\n",
       "0  03/01/2016   BJ            8     5\n",
       "1  17/01/2016   BJ           12     2\n",
       "2  31/01/2016   BJ           19     2\n",
       "3  14/02/2016   BJ           -3     3\n",
       "4  28/02/2016   BJ           19     2\n",
       "5  13/03/2016   BJ            5     3"
      ]
     },
     "execution_count": 283,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g.get_group('BJ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 284,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"8\" halign=\"left\">temperature</th>\n",
       "      <th colspan=\"8\" halign=\"left\">wind</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>city</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>BJ</th>\n",
       "      <td>6.0</td>\n",
       "      <td>10.000</td>\n",
       "      <td>8.532292</td>\n",
       "      <td>-3.0</td>\n",
       "      <td>5.75</td>\n",
       "      <td>10.0</td>\n",
       "      <td>17.25</td>\n",
       "      <td>19.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>2.833333</td>\n",
       "      <td>1.169045</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.00</td>\n",
       "      <td>2.5</td>\n",
       "      <td>3.00</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GZ</th>\n",
       "      <td>4.0</td>\n",
       "      <td>8.750</td>\n",
       "      <td>11.842719</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.50</td>\n",
       "      <td>5.5</td>\n",
       "      <td>13.75</td>\n",
       "      <td>25.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>1.414214</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.50</td>\n",
       "      <td>4.5</td>\n",
       "      <td>5.00</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SH</th>\n",
       "      <td>8.0</td>\n",
       "      <td>4.625</td>\n",
       "      <td>12.489281</td>\n",
       "      <td>-10.0</td>\n",
       "      <td>-5.25</td>\n",
       "      <td>2.0</td>\n",
       "      <td>17.50</td>\n",
       "      <td>20.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>3.625000</td>\n",
       "      <td>1.060660</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.00</td>\n",
       "      <td>3.5</td>\n",
       "      <td>4.25</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SZ</th>\n",
       "      <td>2.0</td>\n",
       "      <td>5.000</td>\n",
       "      <td>21.213203</td>\n",
       "      <td>-10.0</td>\n",
       "      <td>-2.50</td>\n",
       "      <td>5.0</td>\n",
       "      <td>12.50</td>\n",
       "      <td>20.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.500000</td>\n",
       "      <td>2.121320</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.75</td>\n",
       "      <td>2.5</td>\n",
       "      <td>3.25</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     temperature                                                    wind  \\\n",
       "           count    mean        std   min   25%   50%    75%   max count   \n",
       "city                                                                       \n",
       "BJ           6.0  10.000   8.532292  -3.0  5.75  10.0  17.25  19.0   6.0   \n",
       "GZ           4.0   8.750  11.842719  -1.0  0.50   5.5  13.75  25.0   4.0   \n",
       "SH           8.0   4.625  12.489281 -10.0 -5.25   2.0  17.50  20.0   8.0   \n",
       "SZ           2.0   5.000  21.213203 -10.0 -2.50   5.0  12.50  20.0   2.0   \n",
       "\n",
       "                                                     \n",
       "          mean       std  min   25%  50%   75%  max  \n",
       "city                                                 \n",
       "BJ    2.833333  1.169045  2.0  2.00  2.5  3.00  5.0  \n",
       "GZ    4.000000  1.414214  2.0  3.50  4.5  5.00  5.0  \n",
       "SH    3.625000  1.060660  2.0  3.00  3.5  4.25  5.0  \n",
       "SZ    2.500000  2.121320  1.0  1.75  2.5  3.25  4.0  "
      ]
     },
     "execution_count": 284,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g.mean()\n",
    "g.count()\n",
    "g.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 285,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>temperature</th>\n",
       "      <th>wind</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>city</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>BJ</th>\n",
       "      <td>31/01/2016</td>\n",
       "      <td>19</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GZ</th>\n",
       "      <td>31/07/2016</td>\n",
       "      <td>25</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SH</th>\n",
       "      <td>27/03/2016</td>\n",
       "      <td>20</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SZ</th>\n",
       "      <td>25/09/2016</td>\n",
       "      <td>20</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            date  temperature  wind\n",
       "city                               \n",
       "BJ    31/01/2016           19     5\n",
       "GZ    31/07/2016           25     5\n",
       "SH    27/03/2016           20     5\n",
       "SZ    25/09/2016           20     4"
      ]
     },
     "execution_count": 285,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#数据聚合技术\n",
    "g.agg('max') #min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 286,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\40375\\AppData\\Local\\Temp\\ipykernel_19448\\4732453.py:5: FutureWarning: ['date'] did not aggregate successfully. If any error is raised this will raise in a future version of pandas. Drop these columns/ops to avoid this warning.\n",
      "  g.agg(foo)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>temperature</th>\n",
       "      <th>wind</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>city</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>BJ</th>\n",
       "      <td>22</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GZ</th>\n",
       "      <td>26</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SH</th>\n",
       "      <td>30</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SZ</th>\n",
       "      <td>30</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      temperature  wind\n",
       "city                   \n",
       "BJ             22     3\n",
       "GZ             26     3\n",
       "SH             30     3\n",
       "SZ             30     3"
      ]
     },
     "execution_count": 286,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def foo(attr):\n",
    "    # print(type(attr))\n",
    "    return attr.max()-attr.min()\n",
    "\n",
    "g.agg(foo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 287,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>temperature</th>\n",
       "      <th>wind</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>city</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>BJ</th>\n",
       "      <td>10.000</td>\n",
       "      <td>2.833333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GZ</th>\n",
       "      <td>8.750</td>\n",
       "      <td>4.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SH</th>\n",
       "      <td>4.625</td>\n",
       "      <td>3.625000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SZ</th>\n",
       "      <td>5.000</td>\n",
       "      <td>2.500000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      temperature      wind\n",
       "city                       \n",
       "BJ         10.000  2.833333\n",
       "GZ          8.750  4.000000\n",
       "SH          4.625  3.625000\n",
       "SZ          5.000  2.500000"
      ]
     },
     "execution_count": 287,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#透视表\n",
    "pd.pivot_table(df1,index=['city'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
